/*
 * Created by JFormDesigner on Thu Jun 27 15:47:56 CST 2024
 */

package scct.hys.ui;

import scct.hys.dao.UserDao;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 26984
 */
public class DlgUserAdd extends JDialog {
    public DlgUserAdd(Window owner) {
        super(owner);
        initComponents();
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here

        String username=txtName.getText();
        String pass=txtPass.getText();
        String mobile=txtMobile.getText();

        int age=Integer.parseInt(txtAge.getModel().getValue().toString());
        String gender=radFemale.isSelected()?"男":"女";
        if (!username.matches("^\\w{3,8}$")){
            JOptionPane.showConfirmDialog(this,"你输入的用户名不合法，请重新输入");
            return;
        }
        if(!pass.matches("^\\w{3,8}$"))
        {
            JOptionPane.showMessageDialog(this, "请输入合法的密码");
            return;
        }
        if(age<10 || age>100)
        {
            JOptionPane.showMessageDialog(this, "请输入正确的年龄");
            return;
        }
        if(!mobile.matches("^1[3-9]\\d{9}$"))
        {
            JOptionPane.showMessageDialog(this,"请输入合法的手机号码");
            return;
        }
        UserDao userDao = new UserDao();

//      判断是否重名
        if (userDao.isDupname(username)){
            JOptionPane.showMessageDialog(this,"用户名已存在，请重新输入");
            return;
        }
        if(userDao.addUser(username,pass,gender,age,mobile)){
            JOptionPane.showMessageDialog(this,"添加成功");
        }
        this.dispose();

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        label1 = new JLabel();
        txtName = new JTextField();
        label2 = new JLabel();
        txtPass = new JTextField();
        label3 = new JLabel();
        txtMobile = new JTextField();
        btnAdd = new JButton();
        btnCancel = new JButton();
        label4 = new JLabel();
        txtAge = new JSpinner();
        label5 = new JLabel();
        btnMale = new JRadioButton();
        radFemale = new JRadioButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u7528\u6237");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(30, 25), label1.getPreferredSize()));
        contentPane.add(txtName);
        txtName.setBounds(120, 18, 165, txtName.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(30, 60), label2.getPreferredSize()));
        contentPane.add(txtPass);
        txtPass.setBounds(120, 53, 165, 30);

        //---- label3 ----
        label3.setText("\u624b\u673a\u53f7");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(30, 95), label3.getPreferredSize()));
        contentPane.add(txtMobile);
        txtMobile.setBounds(120, 88, 165, 30);

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0");
        btnAdd.addActionListener(e -> btnAdd(e));
        contentPane.add(btnAdd);
        btnAdd.setBounds(new Rectangle(new Point(55, 195), btnAdd.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        contentPane.add(btnCancel);
        btnCancel.setBounds(new Rectangle(new Point(215, 195), btnCancel.getPreferredSize()));

        //---- label4 ----
        label4.setText("\u5e74\u9f84");
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(30, 125), label4.getPreferredSize()));
        contentPane.add(txtAge);
        txtAge.setBounds(120, 118, 165, 30);

        //---- label5 ----
        label5.setText("\u6027\u522b");
        contentPane.add(label5);
        label5.setBounds(new Rectangle(new Point(30, 155), label5.getPreferredSize()));

        //---- btnMale ----
        btnMale.setText("\u7537");
        btnMale.setSelected(true);
        contentPane.add(btnMale);
        btnMale.setBounds(new Rectangle(new Point(140, 153), btnMale.getPreferredSize()));

        //---- radFemale ----
        radFemale.setText("\u5973");
        contentPane.add(radFemale);
        radFemale.setBounds(new Rectangle(new Point(215, 153), radFemale.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(null);

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(btnMale);
        buttonGroup1.add(radFemale);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JLabel label1;
    private JTextField txtName;
    private JLabel label2;
    private JTextField txtPass;
    private JLabel label3;
    private JTextField txtMobile;
    private JButton btnAdd;
    private JButton btnCancel;
    private JLabel label4;
    private JSpinner txtAge;
    private JLabel label5;
    private JRadioButton btnMale;
    private JRadioButton radFemale;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
